const WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
const request = require("../../utils/request.js").request;
const url = require("../../utils/request.js").commonUrl;
const collectRequest = require("../../utils/util.js").collectRequest;
const collectUtil = require("../../utils/collect-util.js");
const util = require("../../utils/util.js");
var actyinfo,op;

Page({

    /**
     * 页面的初始数据
     */

    data: {
        url: url,
        activityVos: "",
        imgUrls: [],
        activityId: '',
        activityAreaId: "",
        latitude: "",
        longitude: "",
        params: {},
        position: "",
        phoneNum: "",
        ticketUrl:"",
        activityId:""
    },
    position: function () {
        wx.redirectTo({
            url: "../place/place?act=" + JSON.stringify(actyinfo)
        })
    },
    //日期格式化
    // formatDate: function (nS) {
    //     return new Date(parseInt(nS)).toLocaleString().substr(0, 10);
    // },
    //时间格式化
    formatDate: function (timeStamp) {
        //return new Date(parseInt(timeStamp) * 1000).toLocaleString();
        let time = new Date(timeStamp);
        let year = time.getFullYear();
        let month = (time.getMonth() + 1);
        let day = (time.getDate());
        let hour = (time.getHours());
        let mimute = (time.getMinutes());
        let second = (time.getSeconds());
        return {
            year, month, day, hour, mimute, second
        }
    },


    detailGo: function (event) {
        var that = this;
        let index = event.currentTarget.dataset.itemid;
        let activityId = this.data.activityVos[index].activityId;
        let activityAreaId = this.data.activityVos[index].activityAreaId;
        var detailInfo = {
            activityId: activityId,
            activityAreaId: activityAreaId
        }

        wx.redirectTo({
            url: '../details/details?detailInfo=' + JSON.stringify(detailInfo),
            success: function (res) {
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })

    },

    like: function () {
        let that = this;
        util.isLogin(function () {

            let result = [], param = {};
            const params = {
                "activityId": that.data.params.activityId, //活动点ID
                "weixin": app.globalData.openid,  //用户ID
                "isInterest": !that.data.params.interest//是否收藏，true为收藏，false为取消收藏
            };
            //console.log(that.data.params);
            collectRequest(params, function (isInterest, count) {


                let isInterestKey = 'params.interest';
                let isInterestValue = isInterest;
                let countKey = 'params.numberOfInterest';
                let countVal = count;
                let interestParam = {}, countParam = {};
                interestParam[isInterestKey] = isInterestValue;
                countParam[countKey] = countVal;
                //console.log(isInterest, count);

                that.setData(interestParam);
                that.setData(countParam);
            });


            app.globalData.flag = that.data.params.activityId;

        })

    },


    getDetail: function (activityId) {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        var params = {
            activityId: activityId,
            weixin: app.globalData.openid
        }

        request('m/activity/get.json', params).then(function (res) {
            wx.hideLoading()
            var data = res.data.result;
            actyinfo = {
                actId: data.activityAreaId,
                curlati: data.latitude,
                curlongi: data.longitude
            }
            console.log(data)
            var content = data.content;
            if (data.telephone) {
                that.setData({
                    phoneNum: data.telephone
                })
            }
            if (data.ticketUrl) {
                that.setData({
                    activityId: data.activityId,
                    ticketUrl: data.ticketUrl
                })
            }
            var imgs = [];
            //console.log("img",res);
            for (var i = 0; i < data.uploadFileVos.length; i++) {
                imgs.push(url + "/upload/get/image.json?url=" + data.uploadFileVos[i].fileUrl)
            }
            that.setData({
                imgUrls: imgs,
                longitude: data.longitude,
                latitude: data.latitude,
                position: {
                    latitude: data.activityAreaVo.latitude,
                    longitude: data.activityAreaVo.longitude,
                    name: data.name,
                    activityId: data.activityId
                },
                params: {
                    name: data.name,
                    beginDate: that.formatDate(data.beginDate).year + '/' + that.formatDate(data.beginDate).month + '/' + that.formatDate(data.beginDate).day,
                    endDate: that.formatDate(data.endDate).year + '/' + that.formatDate(data.endDate).month + '/' + that.formatDate(data.endDate).day,
                    activityLabel: data.activityLabel,
                    address: data.activityAreaVo.areaName,
                    content: WxParse.wxParse('content', 'html', content, that),
                    interest: data.interest,
                    activityId: data.activityId,
                    isCharing: data.charging,
                    detailAddress: data.activityAreaVo.address,
                    numberOfInterest: data.numberOfInterest
                }
            });
        })
    },
    //调用导航
    nav: function () {
        var that = this;
        wx.openLocation({ //出发wx.openLocation API
            latitude: Number(that.data.latitude), //坐标纬度（从地图获取坐标）
            longitude: Number(that.data.longitude), //坐标经度（从地图获取坐标）
            name: that.data.params.address, //打开后显示的地址名称
            address: that.data.params.detailAddress//打开后显示的地址汉字
        })
    },
    //呼叫电话
    phone: function () {
        var that = this;
        wx.showActionSheet({
            itemList: ["联系商家" + ":" + that.data.phoneNum],
            success: function (res) {
                wx.makePhoneCall({
                    phoneNumber: that.data.phoneNum
                })
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },
    getCurAct: function (activityAreaId) {
        let that = this;
        var params = {
            activityAreaId: activityAreaId,
            weixin: app.globalData.openid
        }

        request('m/activityarea/get.json', params).then(function (res) {
            var data = res.data.result;
            that.setData({
                activityVos: data.activityVos
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        op = JSON.parse(options.detailInfo);
        //console.log(op)
        this.setData({
            activityId: op.activityId,
            activityAreaId: op.activityAreaId
        }, function () {
            that.getDetail(that.data.activityId);
            that.getCurAct(that.data.activityAreaId)
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            path: '/pages/details/details?detailInfo=' + JSON.stringify(op)
        }
    }
})
